<template>
	<view>
	  <view class="tpp">
	    <view class=" vsoaas"  @click="saoyisao">
	      <image src="https://ayh.wissun.net/static/ayh/dqd/a5.png" mode="widthFix"/>
	      <view class="saoyisaodtx">扫一扫</view>
	    </view>
	    <view class="textdd">
	        <view class="tupitem">退货单号：{{orderno}}</view>
	        <view class="tupitem">产品信息：</view>
	    </view>
	    <view v-for="(item,index) in productlist" class="prdclass" >
	          <view class="he35">产品名称：{{item.ProductName}}</view>
	          <view class="he35">产品编号：{{item.ProductCD}}</view>
	          <view class="he35">产品批次：{{item.BatchNO}}</view>
	          <view class="he35">产品罐底码：{{item.TrackCode}}</view>
	          <view class="deletebtn" @click="delete_item(index)"  >删除</view>
	    </view>
	    <view class="button" @click="tijiaodata">上传审批</view>
	  </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			  productlist:[],
			  product:[],
			  orderno:""
			}
		},
		onLoad(p) {
			var orderno=this.generateOrderNumber();
			this.orderno=orderno
		},
		onShow() {
	
		},
		methods: {
		   generateOrderNumber() {
		        const date = new Date();
		        const year = date.getFullYear();
		        const month = (date.getMonth() + 1).toString().padStart(2, '0');
		        const day = date.getDate().toString().padStart(2, '0');
		        const hours = date.getHours().toString().padStart(2, '0');
		        const minutes = date.getMinutes().toString().padStart(2, '0');
		        const seconds = date.getSeconds().toString().padStart(2, '0');
		        const milliseconds = date.getMilliseconds().toString().padStart(3, '0');
		        const randomNumber = Math.floor(1000 + Math.random() * 9000);
		     
		        return `T${year}${month}${day}${hours}${minutes}${seconds}${milliseconds}${randomNumber}`;
		    },
		    handle_product(data){
		        var code=[];var product=[];
		        var productlist=this.data.productlist;
		        for(var i=0;i<productlist.length;i++){
		            product.push(productlist[i])
		        }
				this.product=product
		    },
		    saoyisao(){
		        var that=this
		        uni.scanCode({
		            success: (res) => {
		                if(res.errMsg=='scanCode:ok'){
		                    var result = res.result
		                    var data={  code:result  }
		                    data.jxs_code=uni.getStorageSync('fahuo_username').user_code
							this.request('/api/duoqudao/get_product_info', data,"post").then(ret => {
								var pu=[];
								for(var i=0;i<ret.data.length;i++){
								   var ch=false;
								   for(var j=0;j<that.productlist.length;j++){
								      if(ret.data[i].TrackCode == that.productlist[j].TrackCode){
								          ch=true;
								          break;
								      }
								   }
								   if(!ch){ pu.push(ret.data[i]);  }
								}
								var productlist=that.productlist.concat(pu)
								that.productlist=productlist;
							})
		                }else{
		                    uni.showToast({  title: '未识别到任何数据！', icon:"none" })
		                }
		            }
		        })
		      },
		    //删除数据
		    delete_item(index){
		          var productlist=this.productlist
		          productlist.splice(index,1)
				  this.productlist=productlist
		      },
		    //提交申请
		    tijiaodata(){
			  if(this.productlist.length == 0){
				  uni.showToast({
					title: '请先扫一扫需要退货的产品',
					icon:"none"
				  })
				  return
			  }
			  var that=this
			  var postdata={}
			  postdata.order_no=this.orderno
			  postdata.jxs_code=uni.getStorageSync('fahuo_username').user_code
			  postdata.jxs_name=uni.getStorageSync('fahuo_username').username
			  postdata.codelist=JSON.stringify(this.productlist);
			  this.request('/api/duoqudao/save_return_apply', postdata,"post").then(ret => {
		             if(ret.code){
		                uni.showToast({ title:'提交成功',icon:"none"})
		                setTimeout(function(){
		                  uni.navigateBack()
		                },2000)
		             }else{
		                uni.showToast({ title:ret.message, icon:"none"})
		             }
		        })
		    }
		}
	}
</script>

<style>
.button{
  height: 38px;
  line-height: 38px;
  font-size: 16px;
  border-radius: 20px;
  width: 180px;
  margin: 10px auto;
  text-align: center;
  position: fixed;
  bottom: 20px;
  left: calc(50% - 90px);
  background-color: #1B764A;
  color: #fff;
}
page{
  background-color: #f5f5f5;
}
.vsoaas{
  width: 110px;
  height: 176px;
  line-height: 25px;
  width: 116px;
  margin: 10px auto;
  text-align: center;
  border-radius: 5px;
}
.saoyisaodtx{
  text-align: center;
}
.vsoaas image{
  width:100%;
}
.tpp{ margin-top: 30px;}
.tupitem{
  padding: 10px 20px;
  border-bottom: 1px solid #eee;
}
.prdclass{
  margin: 10px;
  background-color: #fff;
  border-radius: 10px;
  padding-left: 10px;
}
.textdd{
  margin: 10px;
  overflow: auto;
  background-color: #fff;
  margin-top: 20px;
  font-size: 14px;
  font-weight: bold;
}
.prdclass{border: 1px solid #eee;padding: 10px;}
.deletebtn{
  height: 30px;
  line-height: 30px;
  width: 75px;
  margin: 0 auto;
  background-color: red;
  color: #fff;
  text-align: center;
  border-radius: 5px;
  margin-bottom: 10px;
  margin-top: 10px;
}
.he35{line-height:25px;font-size: 14px;}
</style>
